package com.penglecode.datafetch.taobao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.Assert;

public class TaoBaoListingProductDataFetcher implements DataFetcher<List<ProductInfo>> {

	private static final Log logger = LogFactory.getLog(TaoBaoListingProductDataFetcher.class);
	
	@SuppressWarnings("unused")
	private String searchPageUrl;
	
	private Document document;

	public TaoBaoListingProductDataFetcher(String searchPageUrl){
		this.searchPageUrl = searchPageUrl;
		try {
			Assert.hasLength(searchPageUrl, "'searchPageUrl' can not be empty!");
			document = Jsoup.connect(searchPageUrl).get();
			//System.out.println(document.html());
		} catch (IOException e) {
			logger.error(e, e);
		}
	}
	
	public List<ProductInfo> doFetching() {
		Elements productListBoxElements = document.select("div.tb-content div.row div.item");
		if(productListBoxElements != null && !productListBoxElements.isEmpty()){
			List<ProductInfo> productInfoList = new ArrayList<ProductInfo>();
			Elements elements = null;
			Element element = null;
			String text = null;
			for(Element productListBoxElement : productListBoxElements){
				ProductInfo product = new ProductInfo();
				//获取productId
				product.setProductId(StringUtils.trim(productListBoxElement.attr("nid")));
				//获取productName
				elements = productListBoxElement.select("div.item-box h3.summary a");
				if(!CollectionUtils.isEmpty(elements)){
					element = elements.get(0);
					product.setProductName(StringUtils.trim(element.text()));
				}
				//获取product picture/detail url
				elements = productListBoxElement.select("div.item-box div.pic p.pic-box a");
				if(!CollectionUtils.isEmpty(elements)){
					element = elements.get(0);
					product.setProductDetailPageUrl(StringUtils.trim(element.attr("href")));
					elements = element.select("img");
					if(!CollectionUtils.isEmpty(elements)){
						element = elements.get(0);
						product.setProductListPictureUrl(StringUtils.trim(element.attr("src")));
					}
				}
				//获取价格
				elements = productListBoxElement.select("div.item-box div.price");
				if(!CollectionUtils.isEmpty(elements)){
					element = elements.get(0);
					text = StringUtils.defaultIfBlank(element.text(), "0");
					Double price = 0.0;
					if(!StringUtils.isBlank(text)){
						text = StringUtils.stripStart(text, "￥");
						if(NumberUtils.isNumber(text)){
							price = Double.parseDouble(text);
						}
					}
					product.setPrice(price);
				}
				//获取运费
				elements = productListBoxElement.select("div.item-box div.shipping");
				if(!CollectionUtils.isEmpty(elements)){
					element = elements.get(0);
					text = StringUtils.defaultIfBlank(element.text(), "0");
					Double price = 0.0;
					if(!StringUtils.isBlank(text) && !"免运费".equals(text)){
						text = StringUtils.stripStart(text, "运费：");
						if(NumberUtils.isNumber(text)){
							price = Double.parseDouble(text);
						}
					}
					product.setShippingFee(price);
				}
				//获取最近成交量
				elements = productListBoxElement.select("div.item-box div.dealing");
				if(!CollectionUtils.isEmpty(elements)){
					element = elements.get(0);
					text = element.text();
					text = StringUtils.stripStart(text, "最近");
					text = StringUtils.stripEnd(text, "人成交");
					if(!StringUtils.isBlank(text) && NumberUtils.isNumber(text)){
						product.setDealingCount(Integer.parseInt(text));
					}
				}
				//获取评论数
				elements = productListBoxElement.select("div.item-box div.count");
				if(!CollectionUtils.isEmpty(elements)){
					element = elements.get(0);
					text = element.text();
					text = StringUtils.stripEnd(text, "条评论");
					if(!StringUtils.isBlank(text) && NumberUtils.isNumber(text)){
						product.setCommentCount(Integer.parseInt(text));
					}
				}
				//获取商铺名称
				elements = productListBoxElement.select("div.item-box div.seller a");
				if(!CollectionUtils.isEmpty(elements)){
					element = elements.get(0);
					text = element.text();
					product.setStoreName(StringUtils.trim(text));
				}
				//获取商铺所在地(短名)
				elements = productListBoxElement.select("div.item-box div.loc");
				if(!CollectionUtils.isEmpty(elements)){
					element = elements.get(0);
					text = element.text();
					product.setStoreShortAddress(StringUtils.trim(text));
				}
				//System.out.println(product);
				productInfoList.add(product);
			}
			return productInfoList;
		}
		return null;
	}
	
	public static void main(String[] args) {
		String keyword = "thinkpad";
		int currentPageIndex = 0;
		int pageSize = 40;
		String searchPageUrl = "http://s.taobao.com/search?initiative_id=staobaoz_20130529&jc=1&q=" + keyword + "&stats_click=search_radio_all%3A1&promote=0&atype=b&filterFineness=2&pspuid=214609695%2C207729829&tab=all&s=" + (currentPageIndex * pageSize) + "#J_relative";
		TaoBaoListingProductDataFetcher fetcher = new TaoBaoListingProductDataFetcher(searchPageUrl);
		List<ProductInfo> productInfoList = fetcher.doFetching();
		System.out.println(">>> size = " + productInfoList.size());
	}
	
}
